package cn.com.wxd.dao.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.dao.HibernateDao;
import cn.com.wxd.entity.user.UsersInfo;
import cn.com.wxd.common.CharUtil;

/**
 * Title:用户信息持久化类
 * Description:用户信息持久化类
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2015年10月22日 下午2:39:46
 *
 * @author WangXuDong
 * @version 1.0
 */
public class UsersDaoImpl extends HibernateDao implements UsersDao {

    private static final Logger log = LoggerFactory.getLogger(UsersDaoImpl.class);

    @Override
    public UsersInfo selectUsersByuserId(long id) {
        Session session = this.currentSession();
        return session.get(UsersInfo.class, id);
    }

    @Override
    public boolean updatePassword(long userId, String password) {
        Session session = this.currentSession();
        try {
            UsersInfo user = session.get(UsersInfo.class, userId);
            if (user == null) {
                return false;
            }
            user.setPassWord(password);
            session.update(user);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    @Override
    public List<UsersInfo> selectUsersInfos(HashMap<String, String> param) {
        Session session = this.currentSession();
        DetachedCriteria dc = DetachedCriteria.forClass(UsersInfo.class);
        Iterator<String> iter = param.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            String value = param.get(key);
            dc.add(Restrictions.eq(key, value));
        }
        Criteria c = dc.getExecutableCriteria(session);
        return c.list();
    }

    @Override
    public int selectUserCount() {
        Session session = this.currentSession();
        Query query = session.createQuery("select count(*) from UsersInfo");
        String count = query.uniqueResult().toString();
        return CharUtil.stringParsInt(count);
    }

    @Override
    public boolean updateUserInfo(long id, HashMap<String, Object> param) {
        Session session = this.currentSession();
        try {
            if (param != null && param.size() > 0) {
                String paramkey = "";
                List paramvalue = new ArrayList();
                for (String key : param.keySet()) {
                    if (param.get(key) != null && !param.get(key).equals("null")) {
                        paramkey += "u." + key + "=?,";
                        paramvalue.add(param.get(key));
                    }
                }
                paramkey = paramkey.substring(0, paramkey.length() - 1);
                String HQL = "UPDATE UsersInfo u SET " + paramkey + " where id = " + id;
                Query query = session.createQuery(HQL);
                for (int i = 0; i < paramvalue.size(); i++) {
                    query.setParameter(i, paramvalue.get(i));
                }
                int reint = query.executeUpdate();
                if (reint == 1) {
                    return true;
                }
            }
        } catch (Exception e) {
            log.error("用户信息更新失败" + e.getMessage(), e);
            return false;
        }
        return false;
    }
}
